package com.aaron.kjviewutils.footballleague;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.aaron.kjviewutils.BaseActivity;
import com.aaron.kjviewutils.R;
import com.aaron.kjviewutils.TabPagerAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 作者：Aaron on 2017/2/17 16:58
 * 邮箱：kaijin.fang@edianniu.com
 */
public class FootBallLleagueActivity extends BaseActivity {

    @Bind(R.id.footballToolBar)
    Toolbar footballToolBar;
    @Bind(R.id.barTitle)
    TextView barTitle;
    @Bind(R.id.leftBarIcon)
    ImageView leftBarIcon;
    @Bind(R.id.tabs)
    TabLayout tabs;
    @Bind(R.id.viewpager)
    ViewPager viewpager;

    public FootBallLleagueBean data;
    private TabPagerAdapter adapter;
    private List<Fragment> fragments;

    private TabLayout.Tab djTab;
    private TabLayout.Tab fjTab;
    private TabLayout.Tab xjTab;
    private TabLayout.Tab ycTab;
    private TabLayout.Tab yjTab;
    private TabLayout.Tab zcTab;

    @OnClick(R.id.leftBarIcon)
    void leftBarIconClick()
    {
        closeSelf(FootBallLleagueActivity.this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.layout_football);
        ButterKnife.bind(this);
        data = (FootBallLleagueBean) this.getIntent().getExtras().getSerializable("data");
        fragments = new ArrayList<Fragment>();
        initTitle();
        initContent();
    }

    private void initContent()
    {
        //德甲
        LleagueFragment dj = new LleagueFragment();
        //法甲
        LleagueFragment fj = new LleagueFragment();
        //西甲
        LleagueFragment xj = new LleagueFragment();
        //英超
        LleagueFragment yc = new LleagueFragment();
        //意甲
        LleagueFragment yj = new LleagueFragment();
        //中超
        LleagueFragment zc = new LleagueFragment();
        Bundle b01 = new Bundle();
        b01.putInt("index", 0);
        dj.setArguments(b01);
        fragments.add(dj);
        Bundle b02 = new Bundle();
        b02.putInt("index", 1);
        fj.setArguments(b02);
        fragments.add(fj);
        Bundle b03 = new Bundle();
        b03.putInt("index", 2);
        xj.setArguments(b03);
        fragments.add(xj);
        Bundle b04 = new Bundle();
        b04.putInt("index", 3);
        yc.setArguments(b04);
        fragments.add(yc);
        Bundle b05 = new Bundle();
        b05.putInt("index", 4);
        yj.setArguments(b05);
        fragments.add(yj);
        Bundle b06 = new Bundle();
        b06.putInt("index", 5);
        zc.setArguments(b06);
        fragments.add(zc);
        adapter = new TabPagerAdapter(getSupportFragmentManager(), fragments);
        viewpager.setAdapter(adapter);
        tabs.setupWithViewPager(viewpager);
        tabs.setTabMode(TabLayout.MODE_SCROLLABLE);
        djTab = tabs.getTabAt(0);
        djTab.setCustomView(getTabsItem("德甲"));
        fjTab = tabs.getTabAt(1);
        fjTab.setCustomView(getTabsItem("法甲"));
        xjTab = tabs.getTabAt(2);
        xjTab.setCustomView(getTabsItem("西甲"));
        ycTab = tabs.getTabAt(3);
        ycTab.setCustomView(getTabsItem("英超"));
        yjTab = tabs.getTabAt(4);
        yjTab.setCustomView(getTabsItem("意甲"));
        zcTab = tabs.getTabAt(5);
        zcTab.setCustomView(getTabsItem("中超"));
    }

    private View getTabsItem(String tag)
    {
        View view = inflater.inflate(R.layout.tas_item, null, false);
        TextView tabsTag = (TextView) view.findViewById(R.id.tabsTag);
        tabsTag.setText(tag);
        return view;
    }

    private void initTitle()
    {
        footballToolBar.setTitle("");
        barTitle.setText("五大联赛");
        loadBackground(leftBarIcon, resources.getDrawable(R.mipmap.back));
        footballToolBar.setNavigationIcon(null);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK
                && event.getAction() == KeyEvent.ACTION_DOWN)
        {
            //关闭
            closeSelf(FootBallLleagueActivity.this);
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }
}
